import collections
class Solution(object):
    def longestSubsequence(self, arr, difference):
        d = collections.defaultdict(int)
        d[arr[0]] = 1
        max_len = 1
        for num in arr[1:]:
            d[num] = max(d[num], d[num - difference] + 1)
            max_len = max(max_len, d[num])
        return max_len
